package activity.login;

import android.os.Bundle;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.hutoubenjg.R;

import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import presenter.UserPresenter;
import util.utls.ByAlert;
import util.utls.IsUtilUid;
import util.volley.ResultEntity;

/**
 * Created by itboye on 2016/12/5.
 */

public class ActivityRestPasswoldSIn extends BaseActivity implements Observer {
    private ToggleButton login_tb;
    private ImageView back;
    private TextView tvTitle;
    private TextView tvTest;
    private boolean isHiden = true;
    EditText res_three_num;
    TextView pass_three_ok;

    private String phone;// 接上页面传来的手机号
    private String code;// 接上页面传来的验证码

    UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_passwold);
        tvTitle.setText("重置密码");
        tvTest.setVisibility(View.GONE);
        userPresenter = new UserPresenter(this);
        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_tb:
                InputType(v);
                break;
            case R.id.back:
                this.finish();
                ;
                break;
            case R.id.pass_three_ok:// 完成
                if (TextUtils.isEmpty(res_three_num.getText().toString())) {
                    ByAlert.alert("请输入密码");
                    return;
                }
                showProgressDialog("    正在加载中...", true);
                userPresenter.updatePass("+86", code, phone, res_three_num.getText().toString());
                break;
            default:
                break;
        }
    }

    /**
     * 设置密码框可见
     *
     * @param view
     */
    public void InputType(View view) {
        if (isHiden) {
            //设置文本可见
            res_three_num.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            login_tb.setBackgroundResource(R.mipmap.yanjing_banned);
        } else {
            //设置文本不可见
            login_tb.setBackgroundResource(R.mipmap.yanjing);
            res_three_num.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
        isHiden = !isHiden;
        res_three_num.postInvalidate();
        //把光标移动到最后面
        CharSequence charSequence = res_three_num.getText();
        if (charSequence instanceof Spannable) {
            Spannable spanText = (Spannable) charSequence;
            Selection.setSelection(spanText, charSequence.length());
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == UserPresenter.updata_pass_success) {
                ByAlert.alert(entity.getData());
                finish();
                try {
                    closeProgressDialog();
                } catch (Exception Exception) {

                }
            }
            if (entity.getEventType() == UserPresenter.updata_pass_fail) {
                ByAlert.alert(entity.getData());
            }
        }
    }
}
